﻿<?php
//Copyright by SONPX - 2010
session_start();
class home extends Controller {
	//language = 2 : Vietnam
	//language = 1 : English
 	protected  $language = 0;
 	
	function home()
	{		
		parent::Controller();
		$this->load->library('session');
		$this->load->library('parser');		
		$this->load->helper('language');
		$this->load->helper('url');
		$this->load->helper('xml'); 
		$data = array();
		$this->load->view('template/header',$data);
		$this->load->view('template/adventive',$data);
		$this->load->view('template/search',$data);

		$this->load->view('template/potsnews',$data);
		
		$this->load->model('news_model');
		$data['cateshow']=$this->news_model->cateshow(0);
		$data['catehide']=$this->news_model->catehide(0);
		$data['banner']=$this->news_model->banner();
		$data['newhot']=$this->news_model->newhot();
		$this->load->view('template/menuhome',$data);
		$this->load->view('template/homecontentleft',$data);
		
		$data['producthot']=$this->news_model->producthot();
		$this->load->view('template/homecontentleftcate1',$data);
		
		$listcate=$this->news_model->ListCateHome();
		foreach($listcate as $row)
		{
			$data['cateid']=$row->cateid;
			$data['catename']=$row->catename;
			$data['productshow']=$this->news_model->product($row->cateid,'',0,5,'');
			$data['producthide']=$this->news_model->product($row->cateid,'',0,5,"price asc");
			$this->load->view('template/homecontentleftcate2',$data);
		}
		echo "</div></div>";
		$this->parser->parse('template/footer', $data,false);
	}
	
	function index()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		$this->load->view('home',$data);
	}
	
	function question()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		$this->load->view('home',$data);
	}
	
	function categame()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		$this->load->view('home',$data);
	}
	
	function cateauction()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		$this->load->view('home',$data);
	}
	
	function catefriend()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		$this->load->view('home',$data);
	}
	
	function cateyte()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		$this->load->view('home',$data);
	}
	
	function catefootball()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		$this->load->view('home',$data);
	}
	
	function categt()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		$this->load->view('home',$data);
	}
	
	function help()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		$this->load->view('home',$data);
	}
	
	public function newlogout()
	{
	//mặc định phải có ở các hàm
		//Khai báo Single Sign On kết nối tới VinaPhone
		$CI =& get_instance(); 
		$this->load->library('phpcas');
		phpcas::setDebug('../../tmp/phpcas.log');
		phpcas::client(CAS_VERSION_2_0,'vinaphone.com.vn',443,'auth');
		phpcas::handleLogoutRequests(true, array('http://vinaphone.com.vn','http://vinaphone.com.vn','http://vinaphone.com.vn'));
		phpcas::setNoCasServerValidation();
		phpcas::forceAuthentication();
		
		$this->session->set_userdata('MSISDN','');
		phpcas::logout();
	}
	
	function indexOut()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$this->connectSSO();
		phpcas::logout();
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		$this->load->view('home',$data);
	}
	
	function read()
	{
		$this->load->model('config_model');
		$data['menus']=$this->config_model->showmenu();
		if($this->input->xss_clean($_GET['ticket'])){  
			$some_variable = $this->input->xss_clean($_GET['ticket']);
		 	$data["ticket"] = $some_variable;
		 	$data['user'] = $this->session->userdata('ticket');
		 	if($some_variable)
		 	{
				$this->setSession($some_variable);
		 		redirect('home/user');
		 	}
		}else {
			redirect('home/index');
		}
		$this->load->view('home',$data);
	}
	
	function user()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$this->connectSSO();
		$phonenumber = $this->session->userdata('MSISDN');
		
		if(isset($phonenumber))
		{ 
			$phonenumber=phpcas::getUser();
			//$phonenumber=$_SESSION["phpcas"]["user"];
			$this->session->set_userdata('MSISDN',$phonenumber);
		}else {			
			$this->session->set_userdata('MSISDN', phpcas::getUser());
			$phonenumber=$this->session->userdata('MSISDN');
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		$RegistryDate;
		$arr = $this->_GetRegistryDate($phonenumber);
		$Time=0;
		$ServiceUse = "";
		if($arr["Service"] == "mr_premium_1")
		{
			$Time=18000;
			$ServiceUse = "V30";
		}
		if($arr["Service"] == "mr_standard_1")
		{
			$Time=6000;
			$ServiceUse = "V15";
		}
		$RegistryDate=$arr["TimeRegistry"];
		$arr2 = $this->_getTimeInfo($phonenumber,$Time,$RegistryDate);
		$data['services'] = $ServiceUse;
		$data['TimeUse'] = $arr2["TimeUse"];
		$data['TimeNotUse'] = $arr2["TimeNotUse"];
		$data['Time'] = $arr["Time"];
		
		$this->load->view('home',$data);
	}
	
	//kiểm tra tình trạng của thuê bao
	public function status()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$this->connectSSO();
		$phonenumber = $this->session->userdata('MSISDN');
		if(isset($phonenumber))
		{ 
			$phonenumber=phpcas::getUser();
			//$phonenumber=$_SESSION["phpcas"]["user"];
			$this->session->set_userdata('MSISDN',$phonenumber);
		}else {			
			$this->session->set_userdata('MSISDN', phpcas::getUser());
			$phonenumber=$this->session->userdata('MSISDN');
		}
		$newphone = substr($phonenumber,2);
		$info = $this->get_data("http://10.16.0.218/GetSubsStatus.html?msisdn=".$newphone);
		$data["info"] = $this->returnStatus($info);
		$data["type"] = $this->typevoicesub($info);
		$data["phone"]= $phonenumber;
		$data['menus']=$this->config_model->showmenu();
		$this->load->view('home',$data);
	}
	
	public function pakageStand($id=null)
	{
		if($id != null)
		{
			//mặc định phải có ở các hàm
			$this->load->model('config_model');
			$data['menus']=$this->config_model->showmenu();
			$this->connectSSO();
			$phonenumber = phpcas::getUser() ;
			
			$newphone = substr($phonenumber,2);
			$random = substr($phonenumber, 4);
			$info = $this->get_data("http://10.16.0.218/GetSubsStatus.html?msisdn=".$newphone);
			$data["info"] = $this->returnStatus($info);
			$data["type"] = $this->typevoicesub($info);
			$data["idtype"] = $id;
			$data["phone"]= $newphone;
			$data["ran"]= $random + date("ss"); 
			
		}else {
			redirect('home/status');
		}
		$this->load->view('home',$data);
	}
	
	public function pakagePremium($id=null)
	{
		if($id != null)
		{
			//mặc định phải có ở các hàm
			$this->load->model('config_model');
			$this->connectSSO();
			$phonenumber = phpcas::getUser() ;
			$newphone = substr($phonenumber,2);
			$random = substr($phonenumber, 4);
			$info = $this->get_data("http://10.16.0.218/GetSubsStatus.html?msisdn=".$newphone);
			$data["info"] = $this->returnStatus($info);
			$data["type"] = $this->typevoicesub($info);
			$data["idtype"] = $id;
			$data["phone"]= $newphone;
			$data["ran"]= $random + date("ss"); 
			$data['menus']=$this->config_model->showmenu();
		}else {
			redirect('home/index');
		}
		$this->load->view('home',$data);
	}
	function register($id)
	{
		if($id != null)
		{
			//mặc định phải có ở các hàm
			$this->load->model('config_model');
			$data['menus']=$this->config_model->showmenu();
			$this->connectSSO();
			$phonenumber = phpcas::getUser() ;
			
			$newphone = substr($phonenumber,2);
			$random = substr($phonenumber, 4);
			$data["ran"]= $random + date("ss");
			$info = $this->get_data("http://10.16.0.218:9004/?from=".$phonenumber."&to=9292&text=sub%20mr_standard_1&channel=WEB&originator=IVR&unique_id=".$random + date("ss"));
			$result = $this->returnStatus($info);
			if($result == "ok")
			{
				redirect('home/status');
			} else {
				redirect('home/status');
			}		 
			
		}else {
			redirect('home/status');
		}
		$this->load->view('home',$data);
	}
	function removeServices()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$this->connectSSO();
		$phonenumber = phpcas::getUser() ;
		$data['menus']=$this->config_model->showmenu();
		
		$newphone = substr($phonenumber,2);
		$random = substr($phonenumber, 4);
		$data["phone"]= $phonenumber;
		$info = $this->get_data("http://10.16.0.218/GetSubsStatus.html?msisdn=".$newphone);
		$type= $this->typevoicesub($info);
		$typeofphone = "";
		if($type=1)
		{$typeofphone = "mr_standard_1";}
		elseif ($type==2)
		{$typeofphone = "mr_premium_2";}

		$info = $this->get_data("http://10.16.0.218:9004/?from=".$phonenumber."&to=9292&text=unsub&nbsp".$typeofphone."&channel=WEB&originator=IVR&unique_id=".$random + date("ss"));

		$data["result"]=$this->returnStatus($info);
		$this->load->view('home',$data);
	}
	
	public function typevoicesub($info)
	{
		//trường hợp đăng ký cả 2 gói 
		if(strstr($info,"mr_standard_1") && strstr($info,"mr_premium_1") ){
			return 3;
		}	
		//trường hợp đăng ký gói standard
		if(strstr($info,"mr_standard_1")){
			return 1;
		}
		//trường hợp đăng ký gói premium
		if(strstr($info,"mr_premium_1")){
			return 2;
		}
		
	}
	
	public function returnStatus($info)
	{
		//ACTIVE 
		if(strstr($info,"ACTIVE"))
		{
			return 1;
		}
		//VALIDATION : đang xử lý 
		if(strstr($info,"VALIDATION"))
		{
			return 2;
		}
		//GRACE 
		if(strstr($info,"GRACE"))
		{
			return 3;
		}
		//PROVISIONING 
		if(strstr($info,"PROVISIONING"))
		{
			return 4;
		}
		//GRACENOCONTENT 
		if(strstr($info,"GRACENOCONTENT"))
		{
			return 5;
		}
		//SUSPEND 
		if(strstr($info,"User is under Suspension Period"))
		{
			return 6;
		}
		//PAUSE 
		if(strstr($info,"PAUSE"))
		{
			return 7;
		}
		//MANDATORY PARAMETER MISSING|400 
		if(strstr($info,"400"))
		{
			return 8;
		}
		//nodata : 403 
		if(strstr($info,"NoData") || strstr($info,"403"))
		{
			return 0;
		}
	}
	
	public function catemusic()
	{
		//mặc định phải có ở các hàm
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		
		$this->load->model('news_model');
		//$values = $this->news_model->LoadMedia($cid,$Table,$IsTop,$IsHot,$IsDown,$IsListen);
		$PageTop=1;
		$PageHot=1;
		$PageDown=1;
		$PageListen=1;
		if (isset($_REQUEST['Top'])) {
			$PageTop=$_REQUEST['Top'];
		}
		if (isset($_REQUEST['Hot'])) {
			$PageHot=$_REQUEST['Hot'];
		}
		if (isset($_REQUEST['Down'])) {
			$PageDown=$_REQUEST['Down'];
		}
		if (isset($_REQUEST['Listen'])) {
			$PageListen=$_REQUEST['Listen'];
		}
		if(!($PageTop>0))
		{
			$PageTop=1;
		}
		if(!($PageHot>0))
		{
			$PageHot=1;
		}
		if(!($PageDown>0))
		{
			$PageDown=1;
		}
		if(!($PageListen>0))
		{
			$PageListen=1;
		}
		$PageSize=10;
		$BeginTop=($PageTop -1)* $PageSize ;
		$BeginHot=($PageHot -1)* $PageSize ;
		$BeginDown=($PageDown -1)* $PageSize ;
		$BeginListen=($PageListen -1)* $PageSize ;
		
		$data['valuesTop'] = $this->news_model->LoadMedia(1,"music",1,0,0,0,$BeginTop,$PageSize);
		$data['valuesHot'] = $this->news_model->LoadMedia(1,"music",0,1,0,0,$BeginHot,$PageSize);
		$data['valuesDown'] = $this->news_model->LoadMedia(1,"music",0,0,1,0,$BeginDown,$PageSize);
		$data['valuesListen'] = $this->news_model->LoadMedia(1,"music",0,0,0,1,$BeginListen,$PageSize);

		$TotalRow=$this->news_model->TotalCount(1,"music",1,0,0,0);		
		$data['TotalTop'] =$this->BuildPage($this->config->item('base_url'). "index.php/en/home/catemusic?Hot=" . (string)$PageHot .  "&Down=" . (string)$PageDown ."&Listen=" . (string)$PageListen,$TotalRow,"Top",$PageTop,$PageSize);
		
		$TotalRow=$this->news_model->TotalCount(1,"music",0,1,0,0);
		$data['TotalHot'] =$this->BuildPage($this->config->item('base_url'). "index.php/en/home/catemusic?Top=" . (string)$PageTop . "&Down=" . (string)$PageDown ."&Listen=" . (string)$PageListen,$TotalRow,"Hot",$PageHot,$PageSize);
		
		$TotalRow=$this->news_model->TotalCount(1,"music",0,0,1,0);
		$data['TotalDown'] =$this->BuildPage($this->config->item('base_url'). "index.php/en/home/catemusic?Top=" . (string)$PageTop . "&Hot=" . (string)$PageHot . "&Listen=" . (string)$PageListen,$TotalRow,"Down",$PageDown,$PageSize);
		
		$TotalRow=$this->news_model->TotalCount(1,"music",0,0,0,1);
		$data['TotalListen'] =$this->BuildPage($this->config->item('base_url'). "index.php/en/home/catemusic?Top=" . (string)$PageTop . "Hot=" . (string)$PageHot .  "&Down=" . (string)$PageDown ,$TotalRow,"Listen",$PageListen,$PageSize);
		
		$this->load->view('home',$data);
	}
	
	public function catestory()
	{
		//mặc định phải có ở các hàm
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		//mặc định phải có ở các hàm
		
		$this->load->model('news_model');
		//$values = $this->news_model->LoadMedia($cid,$Table,$IsTop,$IsHot,$IsDown,$IsListen);
		$PageNew=1;
		$PageListen=1;
		if (isset($_REQUEST['New'])) {
			$PageGhosh=$_REQUEST['New'];
		}
		if (isset($_REQUEST['Listen'])) {
			$PageListen=$_REQUEST['Listen'];
		}
		if(!($PageNew>0))
		{
			$PageNew=1;
		}
		if(!($PageListen>0))
		{
			$PageListen=1;
		}
		$PageSize=10;
		$BeginNew=($PageNew -1)* $PageSize ;
		$BeginListen=($PageListen -1)* $PageSize ;
	
		$data['valuesNew'] = $this->news_model->LoadMedia(0,"Story",0,0,0,0,$BeginNew,$PageSize);
		$data['valuesListen'] = $this->news_model->LoadMedia(0,"Story",0,0,0,1,$BeginListen,$PageSize);

		$TotalRow=$this->news_model->TotalCount(0,"Story",0,0,0,0);
		$data['TotalNew'] =$this->BuildPage($this->config->item('base_url'). "index.php/en/home/catestory?Listen=" . (string)$PageListen,$TotalRow,"New",$PageNew,$PageSize);
		
		$TotalRow=$this->news_model->TotalCount(0,"Story",0,0,0,1);
		$data['TotalListen'] =$this->BuildPage($this->config->item('base_url'). "index.php/en/home/catestory?New=" . (string)$PageNew ,$TotalRow,"Listen",$PageListen,$PageSize);
		
		$this->load->view('home',$data);
	}
	
	public function searchnhac(){
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		
		$this->load->model('news_model');
		$Page=1;
		$SearchContent='';
		if (isset($_REQUEST['Search'])) {
			$SearchContent=$_REQUEST['Search'];
		}
		if (isset($_REQUEST['page'])) {
			$Page=$_REQUEST['page'];
		}
		if(!($Page>0))
		{
			$Page=1;
		}
		$PageSize=10;
		$BeginNew=($Page -1)* $PageSize ;
		
		$data['valuesSearch'] = $this->news_model->LoadSearchMedia(0,"music",$SearchContent,$BeginNew,$PageSize);
		$data['SearchContent']=$SearchContent;
		$TotalRow=$this->news_model->TotalSearchCount(0,"music",$SearchContent);
		$data['TotalRow']=$TotalRow;
		$data['TotalSearch'] =$this->BuildPage($this->config->item('base_url'). "index.php/en/home/searchnhac?Search=".$SearchContent,$TotalRow,"page",$Page,$PageSize);
		$this->load->view('home',$data);
	}
	
	
	public function searchtruyen(){
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] =0;
		$data['user'] = 0;
		
		$this->load->model('news_model');
		$Page=1;
		$SearchContent='';
		if (isset($_REQUEST['Search'])) {
			$SearchContent=$_REQUEST['Search'];
		}
		if (isset($_REQUEST['page'])) {
			$Page=$_REQUEST['page'];
		}
		if(!($Page>0))
		{
			$Page=1;
		}
		$PageSize=10;
		$BeginNew=($Page -1)* $PageSize ;
		
		$data['valuesSearch'] = $this->news_model->LoadSearchMedia(0,"Story",$SearchContent,$BeginNew,$PageSize);
		$data['SearchContent']=$SearchContent;
		$TotalRow=$this->news_model->TotalSearchCount(0,"Story",$SearchContent);
		$data['TotalRow']=$TotalRow;
		$data['TotalSearch'] =$this->BuildPage($this->config->item('base_url'). "index.php/en/home/searchtruyen?Search=".$SearchContent,$TotalRow,"page",$Page,$PageSize);
		$this->load->view('home',$data);
	}
	
	public function loginSSO()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$this->connectSSO();
		//$_SESSION['MSISDN']=phpcas::getUser() ;
		//$phonenumber = phpcas::getUser() ;
		$phonenumber = $this->session->userdata('MSISDN');
		
		if(isset($phonenumber))
		{ 
			$phonenumber=phpcas::getUser();
			//$phonenumber=$_SESSION["phpcas"]["user"];
			$this->session->set_userdata('MSISDN',$phonenumber);
		}else {			
			$this->session->set_userdata('MSISDN', phpcas::getUser());
			$phonenumber=$this->session->userdata('MSISDN');
		}
		
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] = $phonenumber;
		//$session = phpcas::getServerLogoutURL();
		//$data["sess"] = $session;
		$this->load->view('home',$data);
	}
	
	
	function connectSSO()
	{
		//Khai báo Single Sign On kết nối tới VinaPhone
		$CI =& get_instance(); 
		$this->load->library('phpcas');
		//phpcas::setDebug('../../tmp/phpcas.log');
		phpcas::client(CAS_VERSION_2_0,'vinaphone.com.vn',443,'auth');
		phpcas::handleLogoutRequests(true, array('http://vinaphone.com.vn','http://vinaphone.com.vn','http://vinaphone.com.vn'));
		phpcas::setNoCasServerValidation();
		phpcas::forceAuthentication();
		//phpcas::setFixedCallbackURL("http://localhost:8888/cas/en/home/loginSSO/");
		if (isset($_REQUEST['logout'])) {
			phpcas::logout();
		}
	}
	
	function setSession($data)
	{
		$newdata = array(
                   'ticket'  => $data,
					'test' => 'fuck'
               );
		$this->session->set_userdata($newdata);
	}
	
	function removeSession()
	{
		$CI =& get_instance(); 
		$this->load->library('phpcas');
		phpcas::setDebug('../../tmp/phpcas.log');
		phpcas::client(CAS_VERSION_2_0,'vinaphone.com.vn',443,'auth');
		phpcas::handleLogoutRequests(true, array('http://vinaphone.com.vn','http://vinaphone.com.vn','http://vinaphone.com.vn'));
		phpcas::setNoCasServerValidation();
		phpcas::forceAuthentication();
		if (isset($_REQUEST['logout'])) {
			phpcas::logout();
		}
		$this->load->model('config_model');
		$data['menus']=$this->config_model->showmenu();
		
		//$this->session->unset_userdata('ticket');
		//$this->session->sess_destroy();
		$this->load->view('home',$data);
	}

	function searchGoogle($keyword)
	{
		
		redirect('http://www.google.com.vn/search?q=' . $keyword);
	}
	
	function intro()
	{
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		//$this->connectSSO();
		//$phonenumber = phpcas::getUser() ;
		//$data["uinfo"] = $phonenumber;
		$data["uinfo"] = 0;
		$this->load->model('news_model');
		$data['intros']=$this->news_model->shownewsbyroot(2);
		
		$this->load->model('news_model');
		
		$data['valuesStandard'] = $this->news_model->PacketInfo("mr_standard_1");
		$data['valuesPremium'] = $this->news_model->PacketInfo("mr_premium_1");
		$this->load->view('home',$data);
	}
	
	function services()
	{		 
		//mặc định phải có ở các hàm
		$this->load->model('config_model');
		$phonenumber = $this->session->userdata('MSISDN');
		if(	strlen($phonenumber)>8){
			$_SESSION["phpcas"]["user"]=$phonenumber;
		}
		$data['menus']=$this->config_model->showmenu();
		$data["uinfo"] = 0;
		$this->load->view('home',$data);
	}
	
	 function _GetRegistryDate($MSISDN)
     {
				$arrResult = array("Time" => "", "Service" => "", "TimeRegistry" => "");
	 			$fname = "http://10.16.0.218:8888/mradio/AccountDetails?msisdn=+" . $MSISDN;
                $xmlRaw = file_get_contents($fname);				 
				$pieces = explode("<date>", $xmlRaw);
                $this->load->library('simplexml');
				$result = "";
				$Service = "";
				$DateRegitry=date("Y-m-d"). " " .date("H:i:s");
				if(count($pieces)>1){
					$arrRegis=explode("</date>", $pieces[1]);
					$DateRegitry= $arrRegis[0];
					$result=$arrRegis[0];
				}
				$s = explode("<servicename>", $xmlRaw);
				if(count($s)>0){
					$ss = explode("</servicename>", @$s[1]);
					$Service = @$ss[0];
				}
				/*
				if(count($pieces)>1)
				{
                	$xmlData = $this->simplexml->xml_parse($xmlRaw);
					foreach($xmlData['member'] as $row)
					{
						$result = $row['date'];
						$Service = $row['servicename'];
	
					}
				}
				*/
				$Arr1 =  explode(" ",$result);
				if(count($Arr1)>1)
				{
					$Arr2 =  explode("-",$Arr1[0]);
					if(count($Arr2)>2)
					{
						$result = (string)$Arr2[2] . "/" . (string)$Arr2[1] . "/" . (string)$Arr2[0] . " " . (string)$Arr1[1];
					}					
				}
				
				$arrResult["Time"]=$result;
				$arrResult["Service"]=$Service;
				$arrResult["TimeRegistry"]=$DateRegitry;
                return $arrResult;
        }
		
		function _getTimeInfo($MSISDN,$Total,$RegistryDate)
     	{
			$arrReturlt= array("TimeUse" => "", "TimeNotUse" => "");
			if(strlen($MSISDN)==11){
				$MSISDN = substr($MSISDN,2);
			}
			
			if(strlen($MSISDN)==12){
				$MSISDN = substr($MSISDN,2);
			}
			$fname = "http://10.16.0.218:8888/mradio/IvrTransDetails?msisdn=" . $MSISDN . "&startdate=" . date('Y') . "-" . date('m') . "-" . "01&enddate=" . date("Y-m-d");
			$xmlRaw = file_get_contents($fname);
			 
			$pieces = explode("<callduration>", $xmlRaw);
			$this->load->library('simplexml');
			$TimeUse=0;
			if(count($pieces)>1)
			{
				$xmlData = $this->simplexml->xml_parse($xmlRaw);
				foreach($xmlData['member'] as $row)
				{	
					if($this->SoSanh($RegistryDate,$row['callstarttime'])==1){					
						$TimeUse = $TimeUse  + (int)$row['callduration'];
					}	
				}
			}
			$arrReturlt["TimeUse"]= $this-> ConterTimeName($TimeUse);
			$arrReturlt["TimeNotUse"]=  $this->ConterTimeName($Total - $TimeUse);
			return $arrReturlt;
        }
		
		function SoSanh($RegistryDate,$TimeUse){
			$Time = explode(" ", $RegistryDate);
			$DayReg=explode("-",$Time[0]);
			$Registry = explode(":",$Time[1]);
			$Time =explode(" ", $TimeUse);
			$DayUse=explode("-",$Time[0]);
			$Use = explode(":",$Time[1]);
			
			if((int)$DayReg[2]>(int)$DayUse[2]){
				return 0;
			}
			if((int)$Registry[0]>(int)$Use[0]){
				return 0;
			}
			if((int)$Registry[1]>(int)$Use[1]){
				return 0;
			}
			if((float)$Registry[2]>(float)$Use[2]){
				return 0;
			}
			return 1;
		}
		
		function ConterTimeName($Second)
		{
			$Hour=0;
			$Minute=0;
			$Minute=floor($Second/60);
			$Second=$Second-floor($Second/60)*60;
			//$Hour=floor($Minute/60);
			//$Minute=$Minute-floor($Minute/60)*60;
			//$Second=$Second%60;
			//return (string)$Hour . " giờ " . (string)$Minute . " phút " . $Second . " giây";
			return (string)$Minute . " phút " . $Second . " giây";
		}

	
	function dskid($cid)
	{
		$this->load->model('news_model');
		$this->load->model('tour_model');
		$this->load->model('config_model');
		$data['menus']=$this->config_model->showmenu();
		$data['rightbar1']=$this->news_model->shownewslimit(50);	
		$data['menus']=$this->config_model->showmenu(1);
		if($cid)
		{
			$data['newsinfo']=$this->news_model->shownews($cid);	
		}else {
			redirect('home/index');
		}
		$this->load->view('home',$data);
	}
	
	function dskiddetail($id)
	{
		$this->load->model('news_model');
		$this->load->model('tour_model');
		$this->load->model('config_model');
		$data['menus']=$this->config_model->showmenu();
		$data['rightbar1']=$this->news_model->shownewslimit(50);	
		$data['menus']=$this->config_model->showmenu(1);
		$data['dangtruycap'] = $this->showCounter();
		if($id)
		{
			$data['newsinfo']=$this->news_model->shownewsbyid($id);	
		}else {
			redirect('home/index');
		}
		$this->load->view('home',$data);
	}
	
	function get_data($url)
	{
		$ch = curl_init();
		$timeout = 5;
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}

	
	function showCounter()
	{
		$filename = "Counter.log";
		$fd = fopen ($filename,"r");
		$contents = fread ($fd, filesize ($filename));
		$a = intval($contents);
		++$a;
		$f = fopen($filename,"w");
		$fw = fwrite($f,$a);
		fclose($f);
		fclose($fd);
		return $a;
	}
	
	
        function BuildPage($PageLink,$TotalRow,$Para,$CurrentPageIndex,$PageSize)
        {
			$TotalPage = 0;
			$MaxPage=10;
			$TotalPage = floor($TotalRow / $PageSize);
			if ($TotalRow % $PageSize > 0)
			{
				$TotalPage++;
			}
			if ($CurrentPageIndex > $TotalPage)
			{
				$CurrentPageIndex = $TotalPage;
			}
			if ($CurrentPageIndex < 1)
			{
				$CurrentPageIndex = 1;
			}
			$BeginRow=($CurrentPageIndex - 1) * $PageSize + 1;;
			$EndRow=($CurrentPageIndex - 1) * $PageSize + $PageSize;
			
			$arr=explode("?",$PageLink);
			if (count($arr) > 1)
			{
				$PageLink .= "&" . $Para . "=";
			}
			else
			{
				$PageLink .= "?" . $Para . "=";
			}
			if($TotalPage<2)
			{
				return "";
			}
			
			if ($TotalPage <=$MaxPage)
			{
				$BeginPage=1;
            }
			elseif ($CurrentPageIndex == 1)
			{
				$BeginPage=1;
			}
			elseif ($CurrentPageIndex == $TotalPage)
			{
				$BeginPage= $TotalPage + 1 - $MaxPage;
			}
			elseif ($CurrentPageIndex + $MaxPage / 2 >= $TotalPage)
			{
				$BeginPage= $TotalPage + 1 - $MaxPage;
			}
			elseif ($CurrentPageIndex - $MaxPage / 2 <= 1)
			{
				$BeginPage= 1;
			}
			else
			{
            	$BeginPage=$CurrentPageIndex - $MaxPage / 2;
			}
			$Return=" <br /><div class=\"jb_pagination\">";
			if ($TotalPage > $MaxPage)
			{
				$Return=$Return . "<a href=\"" . (string)PageLink . "1\">Đầu</a>";
				for ($i = 0; $i < $MaxPage; $i++)
				{
					if ($BeginPage + $i == $CurrentPageIndex)
					{
						$Return=$Return . "<a class=\"p_current\" href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
					}
					else
					{
						$Return=$Return . "<a href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
					}
				}
				$Return=$Return . "<a href=\"" . (string)$PageLink . (string)$TotalPage . "\">Cuối</a>";
			}
			else
			{
				for ($i = 0; $i < $TotalPage; $i++)
				{
					if ($BeginPage + $i == $CurrentPageIndex)
					{
						$Return=$Return . "<a class=\"active\" href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
					}
					else
					{
						$Return=$Return . "<a href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
					}
				}
			}

			$Return=$Return . "</div><br /><br />";

			return $Return;
        }
}
?>